package com.snpic.appaw.module.insure.service.cxParty;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.snpic.appaw.module.insure.controller.app.cxParty.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.cxParty.CxPartyDO;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.util.object.BeanUtils;

import com.snpic.appaw.module.insure.dal.mysql.cxParty.CxPartyMapper;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.framework.common.util.collection.CollectionUtils.convertList;
import static com.snpic.appaw.framework.common.util.collection.CollectionUtils.diffList;
import static com.snpic.appaw.module.insure.enums.ErrorCodeConstants.*;

/**
 * 车险出单-投被保人信息 Service 实现类
 *
 * @author jijun
 */
@Service
@Validated
public class CxPartyServiceImpl implements CxPartyService {

    @Resource
    private CxPartyMapper cxPartyMapper;

    @Override
    public Long createCxParty(AppCxPartySaveReqVO createReqVO) {
        // 插入
        CxPartyDO cxParty = BeanUtils.toBean(createReqVO, CxPartyDO.class);
        cxPartyMapper.insert(cxParty);

        // 返回
        return cxParty.getId();
    }

    @Override
    public void updateCxParty(AppCxPartySaveReqVO updateReqVO) {
        // 校验存在
        validateCxPartyExists(updateReqVO.getId());
        // 更新
        CxPartyDO updateObj = BeanUtils.toBean(updateReqVO, CxPartyDO.class);
        cxPartyMapper.updateById(updateObj);
    }

    @Override
    public void deleteCxParty(Long id) {
        // 校验存在
        validateCxPartyExists(id);
        // 删除
        cxPartyMapper.deleteById(id);
    }

    @Override
        public void deleteCxPartyListByIds(List<Long> ids) {
        // 删除
        cxPartyMapper.deleteByIds(ids);
        }


    private void validateCxPartyExists(Long id) {
        if (cxPartyMapper.selectById(id) == null) {
            throw exception(CX_PARTY_NOT_EXISTS);
        }
    }

    @Override
    public CxPartyDO getCxParty(Long id) {
        return cxPartyMapper.selectById(id);
    }

    @Override
    public PageResult<CxPartyDO> getCxPartyPage(AppCxPartyPageReqVO pageReqVO) {
        return cxPartyMapper.selectPage(pageReqVO);
    }

}